import 'package:freemusic/model/AbstractSong.dart';
import 'package:freemusic/model/Lyric.dart';

class MiguSong extends AbstractSong {
  String copyrightId;

  @override
  List<AbstractSong> fromRemoteJosn(Map json) {
    final jsonMap = json;
    List musics = jsonMap["musics"];
    List<MiguSong> list = List();
    musics.forEach((ele) {
      MiguSong song = MiguSong();
      String id = ele['id'];
      song.hash = "migu_$id";
      song.img = ele['cover'];
      song.isLocal = false;
      song.platform = SongPlatform.migu;
      song.playUrl = ele['mp3'];
      song.singerName = ele['singerName'];
      song.songName = ele['songName'];
      song.timeLength = Duration(milliseconds: 0);
      song.type = "mp3";
      song.copyrightId = ele['copyrightId'];
      list.add(song);
    });
    return list;
  }

  @override
  Lyric stringToLyric() {
    Lyric lyric = Lyric();
    if (lyrics == null) {
      return lyric;
    }
    List<String> list = lyrics.split("\r\n");
    list.forEach((item) {
      if (!item.endsWith("]") && item.isNotEmpty) {
        lyric.slices.add(stringToLyricSlice(item));
      }
    });
    return lyric;
  }

  LyricSlice stringToLyricSlice(String line) {
    LyricSlice lyricSlice = LyricSlice();
    lyricSlice.slice = line.substring(10);
    int minute = int.parse(line.substring(1, 3));
    int second = int.parse(line.substring(4, 6));
    int millis = int.parse(line.substring(7, 9));

    // lyricSlice.milliseconds = minute * 60 * 1000 + second * 1000 + millis;
    lyricSlice.milliseconds = Duration(minutes: minute, seconds: second, milliseconds: millis);
    return lyricSlice;
  }

  @override
  Map toJsonMap() {
    Map map = super.toJsonMap();
    map["copyrightId"] = this.copyrightId;
    return map;
  }

  @override
  void fromDataBase(Map map) {
    super.fromDataBase(map);
    this.copyrightId = map['copyrightId'];
  }
}
